package com.ioc.business.job.controller;


import cn.hutool.core.util.ObjectUtil;
import com.ioc.business.job.domain.JobNodeUseTime;
import com.ioc.business.job.domain.bo.node.use.time.JobNodeUseTimeBo;
import com.ioc.business.job.domain.bo.node.use.time.JobNodeUseTimeQueryBo;
import com.ioc.business.job.service.IJobNodeUseTimeService;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimeImportVo;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimePageVo;
import com.ioc.business.job.vo.node.use.time.JobNodeUseTimeVo;
import com.ioc.common.annotation.Log;
import com.ioc.common.core.controller.BaseController;
import com.ioc.common.core.domain.AjaxResult;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.enums.BusinessType;
import com.ioc.common.utils.poi.ExcelUtil;
import com.ioc.common.validate.AddGroup;
import com.ioc.common.validate.EditGroup;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 作业节点用时表 前端控制器
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-08
 */
@RestController
@RequestMapping("/job-node-use-time")
@AllArgsConstructor
public class JobNodeUseTimeController extends BaseController {
    private final IJobNodeUseTimeService jobNodeUseTimeService;

    /**
     * 分页查询作业节点用时列表
     *
     * @param queryBo
     * @return
     * @folder 作业节点用时/分页查询作业节点用时列表
     */
    @GetMapping("/list")
    public TableDataInfo selectJobNodeUseTimePageList(JobNodeUseTimeQueryBo queryBo) {
        startPage();
        TableVo<JobNodeUseTime, JobNodeUseTimePageVo> data = jobNodeUseTimeService.selectJobNodeUseTimePageList(queryBo);
        return getDataTable(data);
    }

    /**
     * 查询作业节点用时详情信息
     *
     * @param jobNodeUseTimeId
     * @return
     * @folder 作业节点用时/查询作业节点用时详情信息
     */
    @GetMapping("/{jobNodeUseTimeId}")
    public AjaxResult getJobNodeUseTimeInfo(@PathVariable("jobNodeUseTimeId") Long jobNodeUseTimeId) {
        JobNodeUseTimeVo jobNodeUseTimeVo = jobNodeUseTimeService.selectJobNodeUseTimeById(jobNodeUseTimeId);
        if (ObjectUtil.isEmpty(jobNodeUseTimeVo)) {
            return error("数据不存在!");
        }
        return success(jobNodeUseTimeVo);
    }

    /**
     * 作业节点用时
     *
     * @param bo
     * @return
     * @folder 作业节点用时/新增作业节点用时
     */
    @Log(title = "作业节点用时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addJobNodeUseTime(@RequestBody @Validated(AddGroup.class) JobNodeUseTimeBo bo) {
        return toAjax(jobNodeUseTimeService.addJobNodeUseTime(bo));
    }

    /**
     * 编辑作业节点用时
     *
     * @param bo
     * @return
     * @folder 作业节点用时/编辑作业节点用时
     */
    @PostMapping("/edit")
    @Log(title = "作业节点用时", businessType = BusinessType.UPDATE)
    public AjaxResult updateJobNodeUseTime(@RequestBody @Validated(EditGroup.class) JobNodeUseTimeBo bo) {

        return toAjax(jobNodeUseTimeService.updateJobNodeUseTime(bo));
    }

    /**
     * 删除作业节点用时
     *
     * @param idList
     * @return
     * @folder 作业节点用时/删除作业节点用时
     */
    @Log(title = "作业节点用时", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(jobNodeUseTimeService.removeJobNodeUseTime(idList));
    }

    /**
     * 下载作业节点用时模版
     *
     * @param response
     * @folder 作业节点用时/下载作业节点用时模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<JobNodeUseTimeImportVo> util = new ExcelUtil<>(JobNodeUseTimeImportVo.class);
        util.importTemplateExcel(response, "作业节点用时导入模版");
    }

    /**
     * 导入作业节点用时数据
     *
     * @param file
     * @return
     * @throws Exception
     * @folder 作业节点用时/导入作业节点用时数据
     */
    @Log(title = "作业节点用时", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<JobNodeUseTimeImportVo> util = new ExcelUtil<>(JobNodeUseTimeImportVo.class);
        List<JobNodeUseTimeImportVo> voList = util.importExcel(file.getInputStream());
        jobNodeUseTimeService.importJobNodeUseTime(voList);
        return success();
    }

}
